//
//  WelcomViewController.m
//  Project6
//
//  Created by ToanDK on 11/13/12.
//  Copyright (c) 2012 DungNP. All rights reserved.
//

#import "WelcomViewController.h"
#import "RepairViewController.h"
#import "InspectViewController.h"
#import "MBProgressHUD.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SettingViewController.h"


@implementation WelcomViewController
@synthesize  username;
@synthesize  password;
@synthesize  udid;
@synthesize  devicename;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // show navigation controller
    self.navigationController.navigationBarHidden =NO;
    self.title =@"Welcome";
    // change title of back button
    UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
                                initWithTitle:@"Logout"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(OnClick_btnBack:)];
    self.navigationItem.leftBarButtonItem = btnBack;
    // add setting button bar item
    UIBarButtonItem *btnSetting = [[UIBarButtonItem alloc]
                                initWithTitle:@"Setting"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(OnClick_btnSetting:)];
    self.navigationItem.rightBarButtonItem = btnSetting;
    
    // set style for buttons
    UIImage* buttonImage =[[UIImage imageNamed:@"greenButton"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    UIImage* buttonImageON =[[UIImage imageNamed:@"greenButtonOn"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
    
    [btnInspect setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [btnInspect setBackgroundImage:buttonImageON forState:UIControlStateHighlighted];
    [btnInspect setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnInspect setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];

    [btnRepair setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [btnRepair setBackgroundImage:buttonImageON forState:UIControlStateHighlighted];
    [btnRepair setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btnRepair setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"greyBackground"]];
}

-(IBAction)OnClick_btnBack:(id)sender
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText =@"Processing";
    
    NSString * kURLLogin = [kBASE_URL stringByAppendingFormat:@"%@",@"account.php?api=%@&action=%@&user=%@&pass=%@&udid=%@&device=%@"];
    NSString * urlString = [NSString stringWithFormat:kURLLogin,kAPI,kACTION_LOGOUT,self.username,self.password,self.udid,self.devicename];
    
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:
                                       NSUTF8StringEncoding]];
    NSLog(@"url:%@",url);
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    [request setDelegate:self];
    [request startAsynchronous];
    
}

-(IBAction)OnClick_btnSetting:(id)sender
{
    NSLog(@"Go to setting screen");
    SettingViewController *settingView = [[SettingViewController alloc] init];
    [self.navigationController pushViewController:settingView animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// go to inspect screen
-(IBAction)btnInspect_Click:(id)sender
{
    NSLog(@"btnInspect click");
    // go to inspect screen
    InspectViewController * inspectCtr = [[InspectViewController alloc] initWithNibName:@"InspectViewController" bundle:nil];
    [self.navigationController pushViewController:inspectCtr animated:YES];
    
}
//Go to repaire screen
-(IBAction)btnRepair_Click:(id)sender
{
    NSLog(@"btnRepair_Click");
    // go to repair screen
    RepairViewController *repairCtr = [[RepairViewController alloc] initWithNibName:@"RepairViewController" bundle:nil];
    [self.navigationController pushViewController:repairCtr animated:YES];
    
}


-(void)requestFinished:(ASIHTTPRequest *)request
{
    
    if (request.responseStatusCode == 400) {
        NSLog(@"Invalid code");
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Logout" message:@"Invalid code" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Logout" message:@"Code already used" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        
    } else if (request.responseStatusCode == 200) {
        NSString *responseString = [request responseString];
        NSLog(@"response string:%@",responseString);
        
            if([responseString isEqualToString:@"TRUE"])
            {
                // back to login screen
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                
                [self.navigationController popViewControllerAnimated:YES];
            }
        
    } else {
        NSLog(@"Unexpected error");
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Logout" message:@"Unexpected error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
    
     [MBProgressHUD hideHUDForView:self.view animated:YES];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"error:%@",error.localizedDescription);
    
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Logout" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}


@end
